package com.movies.service.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.movies.service.common.Result;
import com.movies.service.entity.Movies;
import com.movies.service.service.MoviesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/user/movies")
public class UserMoviesController {

    @Autowired
    private MoviesService moviesService;

//    分页查询电影列表（支持名称模糊、类型、状态筛选）
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") Integer pageNo,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       @RequestParam(required = false) String mname,
                       @RequestParam(required = false) String type,
                       @RequestParam(required = false) String status) {
        LambdaQueryWrapper<Movies> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(mname != null && !mname.isEmpty(), Movies::getMname, mname)
                .eq(type != null && !type.isEmpty(), Movies::getType, type)
                .eq(status != null && !status.isEmpty(), Movies::getStatus, status);
        Page<Movies> page = moviesService.page(new Page<>(pageNo, pageSize), wrapper);
        return Result.success(page);
    }

//    关键字搜索电影（按名称或类型）
    @GetMapping("/search")
    public Result search(@RequestParam String keyword,
                         @RequestParam(defaultValue = "1") Integer pageNo,
                         @RequestParam(defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<Movies> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Movies::getMname, keyword).or().like(Movies::getType, keyword);
        Page<Movies> page = moviesService.page(new Page<>(pageNo, pageSize), wrapper);
        return Result.success(page);
    }

//    获取电影详情
    @GetMapping("/{id}")
    public Result get(@PathVariable Long id) {
        return Result.success(moviesService.getById(id));
    }
}